﻿using IntelligentAgriculture.Domain.Entity;
using IntelligentAgriculture.Domain.Interface;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IntelligentAgriculture.Domain.Impl
{
    public class DepartMentRepository: IDepartMentRepository
    {

        /// <summary>
        /// 数据库上下文
        /// </summary>
        private readonly AgricultureDbContext context;
        public DepartMentRepository(AgricultureDbContext context)
        {
            this.context = context;
        }

        /// <summary>
        /// 获取所有部门信息
        /// </summary>
        /// <returns></returns>
        public async Task<IQueryable<Department>> DepartMnet()
        {
            return context.Departments.Where(c => c.Isdelete == 0).AsQueryable();
        }


        /// <summary>
        /// 获取部门列表信息
        /// </summary>
        /// <param name="departName"></param>
        /// <param name="departTypeId"></param>
        /// <param name="departState"></param>
        /// <returns></returns>
        public async Task<List<Department>> DepartMnetList(string departName, int departTypeId, int departState)
        {

            var departs = context.Departments.Where(c => c.Isdelete == 0).AsQueryable().AsNoTracking();
            if(!string.IsNullOrEmpty(departName))
            {
                departs = departs.Where(c => c.DepartName.Contains(departName));
            }
            if(departTypeId>0)
            {
                departs = departs.Where(c => c.DepartTypeId == departTypeId);
            }
            if(departState>0)
            {
                departs = departs.Where(c => c.DepartState == departState);
            }
            return await departs.ToListAsync();

        }


        /// <summary>
        /// 添加部门信息
        /// </summary>
        /// <param name="department"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public async Task<int> Add(Department department)
        {
            var departments = await context.Departments.FirstOrDefaultAsync(c => c.DepartName == department.DepartName);
            if (departments!= null)
            {
                throw new Exception("部门名称已存在");
            }
            context.Departments.Add(department);
            return await context.SaveChangesAsync();
        }


        /// <summary>
        /// 树形列表
        /// </summary>
        /// <param name="pid"></param>
        /// <returns></returns>
        public async Task<List<MenuList>> MenuLIst(int pid)
        {
            var menuList = await context.Departments
                              .Where(c => c.Pid == pid)
                              .ToListAsync();

            var list = new List<MenuList>();

            foreach (var department in menuList)
            {
                var menuListItem = new MenuList
                {
                    value = department.DepartId,
                    label = department.DepartName
                };

                // 使用异步递归获取子部门列表
                menuListItem.children = await MenuLIst(department.DepartId);

                list.Add(menuListItem);
            }

            return list;

        }



        /// <summary>
        /// 删除部门信息
        /// </summary>
        /// <param name="departId"></param>
        /// <returns></returns>
        public async Task<int> Delete(int departId)
        {
            var sheep =context.Departments.Find(departId);
            sheep.Isdelete = 1;
            return await context.SaveChangesAsync();
        }






    }





}
